package com.chiu.cblog.view;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.chiu.cblog.R;

/**
 * Created by chiu on 15/12/8.
 */
public class CircleImageView extends ImageView {

    private static final ScaleType IMAGE_SCALE_TYPE = ScaleType.CENTER_CROP;

    private static final int COLOR_DRAWABLE_DEMISION = 2;
    private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;

    private int mBorderWidth;
    private int mBorderColor;
    private int mFillColor;

    private RectF mBorderRect;
    private RectF mBitmapRect;

    private Bitmap mBitmap;
    private BitmapShader mBitmapShader;
    private int mBitmapWidth;
    private int mBitmapHeight;

    private Paint mBitmapPaint;
    private Paint mBorderPaint;
    private Paint mFillPaint;

    private float mBitmapRadius;
    private float mBorderRadius;



    public CircleImageView(Context context) {
        this(context, null);
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    CircleImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        final Resources resources = context.getResources();
        mBorderColor = resources.getColor(R.color.circle_border_default_color);
        mFillColor = resources.getColor(R.color.circle_fill_default_color);
        mBorderWidth = resources.getDimensionPixelOffset(R.dimen.circle_border_width);

        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.CircleImageView,defStyle,0);
        mBorderWidth = array.getDimensionPixelOffset(R.styleable.CircleImageView_circleBorderWidth, mBorderWidth);
        mBorderColor = array.getColor(R.styleable.CircleImageView_circleBorderColor, mBorderColor);
        mFillColor = array.getColor(R.styleable.CircleImageView_circleFillColor, mFillColor);

        array.recycle();

        super.setScaleType(IMAGE_SCALE_TYPE);

        mBitmapPaint = new Paint();
        mBorderPaint = new Paint();
        mFillPaint = new Paint();

        mBitmapRect = new RectF();
        mBorderRect = new RectF();

        init();
    }

    private void init() {

        if(mBitmap == null) {
            return;
        }

        mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

        mBitmapPaint.setShader(mBitmapShader);

        mBorderPaint.setAntiAlias(true);
        mBorderPaint.setColor(mBorderColor);
        mBorderPaint.setStyle(Paint.Style.STROKE);
        mBorderPaint.setStrokeWidth(mBorderWidth);

        mFillPaint.setAntiAlias(true);
        mFillPaint.setColor(mFillColor);
        mFillPaint.setStyle(Paint.Style.FILL);

        mBitmapWidth = mBitmap.getWidth();
        mBitmapHeight = mBitmap.getHeight();

        mBorderRect.set(0, 0, mBitmapWidth, mBitmapHeight);
        mBorderRadius = Math.min((mBitmapWidth - mBorderWidth) / 2.0f , (mBitmapHeight - mBorderWidth) / 2.0f);

        mBitmapRect.set(mBorderRect);
        mBitmapRadius = Math.min(mBitmapWidth / 2.0f, mBitmapHeight / 2.0f);

    }

    @Override
    protected void onDraw(Canvas canvas) {

        if(mBitmap == null) {
            return;
        }

        //draw bitmap
        canvas.drawCircle(mBitmapWidth, mBitmapHeight, mBitmapRadius, mBitmapPaint);

        //draw border
        canvas.drawCircle(mBitmapWidth, mBitmapHeight, mBorderRadius, mBorderPaint);

    }


}
